<?php
require_once('config.php');
 
 /**
 * @version		$Id: out.php 2011.08.20 thierry $
 * @package		site.models
 * @subpackage	com_projid
 * @copyright	Copyright (C) 2011 Pojid All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
 
defined('_JEXEC') or die('Restricted access');
$rptdesign = $_GET['file'];
$out = $_GET['out'];

//acces to report
$myreport=JPATH_BASE."/media/com_projid/reports/".$rptdesign.".rptdesign";

require_once('birtfiller.php');
$mybirt = new BirtFiller($rptdesign);
if($out == 'pdf'){
	header("Content-type: application/pdf");
	header("Content-Disposition: attachment; filename=$rptdesign.pdf");
	}else{
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=$rptdesign.xls");
	}

define ("JAVA_HOSTS", "127.0.0.1:8080");
define ("JAVA_SERVLET", "/JavaBridge/JavaBridge.phpjavabridge");

require_once("$tomcatpath");

if(session_id() == "")
     session_start(); 

$here = getcwd();

$ctx = java_context()->getServletContext();
$birtReportEngine =        java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());


try{


$report = $birtReportEngine->openReportDesign("$myreport");
$task = $birtReportEngine->createRunAndRenderTask($report);
$mybirt->setParameter($task);
if($out == 'pdf'){
$taskOptions = new java("org.eclipse.birt.report.engine.api.PDFRenderOption");
	}else{
	$taskOptions = new java("org.eclipse.birt.report.engine.api.EXCELRenderOption");
	}
$outputStream = new java("java.io.ByteArrayOutputStream");
$taskOptions->setOutputStream($outputStream);
if($out == 'pdf'){
	$taskOptions->setOption("pdfRenderOption.pageOverflow", "pdfRenderOption.fitToPage");
	$taskOptions->setOutputFormat("pdf");
	}else{
	//$taskOptions
	$taskOptions->setOutputFormat("xls");
	}

$task->setRenderOption( $taskOptions );
$task->run();
$task->close();

} catch (JavaException $e) {
	echo $e; //"Error Calling BIRT";
	
}

//echo $outputStream;
echo java_values($outputStream->toByteArray());



?>
